<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>闭包</title>
</head>
<body>
<script>
    // function add(a,b){
    //     if(b===undefined){
    //         b=10;
    //     }
    //     return a+b;
    // }
    // var res = add(1);
    // console.log(res);
    //计数器困境问题
    ////全局变量
    var a = (function (){
        var counter = 0; //只调用一次
        return  function(){
            counter++;
            return counter;
        }
    })();
    console.log(a());//1
    console.log(a());//2
    console.log(a());//3
    console.log(a());//4

</script>
</body>
</html>